package com.cbd.dao.impl;


import java.math.BigInteger;
import java.util.List;


import java.util.List;
import java.util.Map;


import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;


import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cbd.dao.ComplaintrecordDao;
import com.cbd.pojo.Complaintrecord;
import com.cbd.util.IQuantity;
@Repository
public class ComplaintrecordDaoImpl implements ComplaintrecordDao {

	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;

	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@Override
	public int addComplaintrecord(int recordId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateComplaintrecord(String ispass,int complaint,int complaintrecordID){
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		System.out.println(ispass+complaint+complaintrecordID);
		//ispass: 1表示投诉成立，0表示投诉失败（直接删除记录） 
		//complaint:1表示包租婆投诉租客/0表示租客投诉包租婆
		//complaintrecordID:投诉记录的ID
		if("1".equals(""+complaint)&&"1".equals(ispass)){
			Query query = session.createSQLQuery("select rt.renterID from complaintrecord cpd"
					+" left join record rd on cpd.recordID=rd.recordID"
					+" left join purpose pps on rd.purposeID=pps.purposeID"
					+" left join renter rt on pps.renterID=rt.renterID"
					+" where cpd.id=?");
			//1.得到租客的ID
			query.setParameter(0, complaintrecordID);
			List<?> lst = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
			Map<String,Object> mapx = (Map<String, Object>) lst.get(0);
			int renterID =(int) mapx.get("renterID");
			
			//2.通过查询的ID得到租客的RP字段
			Query query2 = session.createSQLQuery("SELECT rt.renterRP FROM renter rt WHERE rt.renterID=?");
			query2.setParameter(0, renterID);
			List<?> lstt = query2.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
			Map<String,Object> map = (Map<String, Object>) lstt.get(0);
			String ladyRP = (String) map.get("renterRP");
			String ft1=ladyRP.split("/")[0];
			String ft2=ladyRP.split("/")[1];
			String resultStr = (Integer.parseInt(ft1)+1)+"/"+ft2;
			
			//3.更新RP字段
			Query query3=session.createSQLQuery("update renter set renterRP=? where renterID=?");
			query3.setParameter(0, resultStr);
			query3.setParameter(1, ""+renterID);
			int resulet1 = query3.executeUpdate();
			
			//4.将记录的未处理变成已处理
			Query query4 = session.createSQLQuery("update complaintrecord cd set cd.type=1 where cd.id=?");
			query4.setParameter(0, complaintrecordID);
			int resulet2 = query4.executeUpdate();
			return resulet1+resulet2;
		}else if("1".equals(""+complaint)&&"0".equals(ispass)){
			Query query5 = session.createSQLQuery("delete from complaintrecord where id=?");
			query5.setParameter(0, complaintrecordID);
			int result3 = query5.executeUpdate();
			return result3;
		}else if("0".equals(""+complaint)&&"1".equals(ispass)){
			Query query = session.createSQLQuery("select ldly.ladyID from complaintrecord com"
					+" left join record rd on com.recordID=rd.recordID"
					+" left join purpose pps on rd.purposeID=pps.purposeID"
					+" left join rentinfo rtif on pps.rentInfoID=rtif.rentInfoID"
					+" left join personalcarport pscp on rtif.carportID=pscp.carportID"
					+" left join landlady ldly on pscp.ladyID=ldly.ladyID"
					+" where com.id=?");
			//1.得到包租婆的ID
			query.setParameter(0, complaintrecordID);
			List<?> lst = query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
			Map<String,Object> mapx = (Map<String, Object>) lst.get(0);
			int landladyID =(int) mapx.get("ladyID");
			
			//2.通过查询的ID得到包租婆的RP字段
			Query query2 = session.createSQLQuery("SELECT ldly.ladyRP FROM landlady ldly WHERE ldly.ladyID=?");
			query2.setParameter(0, landladyID);
			List<?> lstt = query2.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
			Map<String,Object> map = (Map<String, Object>) lstt.get(0);
			String ladyRP = (String) map.get("ladyRP");
			String ft1=ladyRP.split("/")[0];
			String ft2=ladyRP.split("/")[1];
			String resultStr = (Integer.parseInt(ft1)+1)+"/"+ft2;
			
			//3.更新RP字段
			Query query3=session.createSQLQuery("update landlady set ladyRP=? where ladyID=?");
			query3.setParameter(0, resultStr);
			query3.setParameter(1, ""+landladyID);
			int resulet1 = query3.executeUpdate();
			
			//4.将记录的未处理变成已处理
			Query query4 = session.createSQLQuery("update complaintrecord cd set cd.type=1 where cd.id=?");
			query4.setParameter(0, complaintrecordID);
			int resulet2 = query4.executeUpdate();
			return resulet1+resulet2;
		}else if("0".equals(""+complaint)&&"0".equals(ispass)){
			Query query5 = session.createSQLQuery("delete from complaintrecord where id=?");
			query5.setParameter(0, complaintrecordID);
			int result3 = query5.executeUpdate();
			return result3;
		}else{
			return 0;
		}
	}


	@Override
	public List<Object[]> showLandlady(int page) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Query query= session.createSQLQuery("SELECT c.id,l.ladyName,ter.renterName,c.createtime FROM landlady l "+
			
			" LEFT JOIN personalcarport p ON l.ladyID=p.ladyID"+ 
			" LEFT JOIN rentinfo r on p.carportID=r.carportID"+ 
			" LEFT JOIN purpose pu on r.rentInfoID=pu.rentInfoID"+
			" LEFT JOIN renter ter on pu.renterID=ter.renterID"+
			" LEFT JOIN record re on pu.purposeID=re.purposeID"+
			" LEFT JOIN complaintrecord c on re.recordID=c.recordID  where c.type=? and c.compaint=? LIMIT ?,?");
		query.setParameter(0, 0);
		query.setParameter(1, 1);
		query.setParameter(2, page);
		query.setParameter(3, IQuantity.pageSize);
		List<Object[]>list= query.list();
		//System.out.println("dao"+list.size());
		return list;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		Query query= session.createSQLQuery("SELECT count(*) FROM landlady l "+
			" LEFT JOIN personalcarport p ON l.ladyID=p.ladyID"+ 
			" LEFT JOIN rentinfo r on p.carportID=r.carportID"+ 
			" LEFT JOIN purpose pu on r.rentInfoID=pu.rentInfoID"+
			" LEFT JOIN renter ter on pu.renterID=ter.renterID"+
			" LEFT JOIN record re on pu.purposeID=re.purposeID"+
			" LEFT JOIN complaintrecord c on re.recordID=c.recordID where c.type=? and c.compaint=?");
		query.setParameter(0, 0);
		query.setParameter(1, 1);
		BigInteger count1= (BigInteger) query.list().get(0);
		String coun=""+count1;
		System.out.println("dao"+Integer.parseInt(coun));
		return Integer.parseInt(coun);
	}

	@Override
	public int addComplaintrecord(Complaintrecord complaintrecord) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		return (int) session.save(complaintrecord);
	}

	@Override
	public int checkIsCompalint(int recordID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from Complaintrecord c where c.record.recordID = ?");
		query.setInteger(0, recordID);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public List<?> findComplaintInfo(int type,int complaintRecordID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createSQLQuery("select psncpt.carportAddress,psncpt.carNO,rtfo.startTime,rtfo.endTime,"
				+" u.userName,ldly.ladyName,ldly.ladyCard,ldly.ladyDescribe,ldly.ladyTel,cptrd.reason,"
				+" us.userName t,rtr.renterName,rtr.renterCard,rtr.renterDescribe,rtr.renterTel"
				+" from complaintrecord cptrd"
				+" LEFT JOIN record rcd ON cptrd.recordID=rcd.recordID"
				+" LEFT JOIN purpose ppse ON rcd.purposeID=ppse.purposeID"
				+" LEFT JOIN rentinfo rtfo ON ppse.rentInfoID=rtfo.rentInfoID"
				+" LEFT JOIN personalcarport psncpt ON rtfo.carportID=psncpt.carportID" 
				+" LEFT JOIN landlady ldly ON psncpt.ladyID=ldly.ladyID" 
				+" LEFT JOIN `user` u ON ldly.userID=u.userID" 
				+" LEFT JOIN renter rtr ON ppse.renterID=rtr.renterID"
				+" LEFT JOIN `user` us ON rtr.userID=us.userID"
				+" where cptrd.compaint=? and cptrd.id=?;");
		query.setParameter(0, type);
		query.setParameter(1, complaintRecordID);
		List<?> rst = query.list();
		return rst;
	}


	@Override
	public Object findRecordIDFromComplaintByRecordID(int recordID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createSQLQuery("select cpr.recordID from complaintrecord cpr "
				+ " where cpr.recordID = ?");
		query.setParameter(0, recordID);
		List list=query.list();
		if(list.size()==0){
			return null;
		}else{
			return  query.list().get(0);
		}
	}
}
